/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Manager_Image_Utils;

import java.io.File;
import java.util.*;
import javax.mail.*;
import javax.activation.*;
import javax.mail.internet.*;
import javax.swing.JOptionPane;

/**
 *
 * @author PHONG
 */
public class SentMail {

    private String sender;
    private String pass_sender;
    private String revicer;
    private String filename_attachment;
    private String subject;
    private String massage;
    private String[] revicer_CC;
    

    public String getSender() {
        return sender;
    }

    public void setSender(String sender) {
        this.sender = sender;
    }

    public String getPass_sender() {
        return pass_sender;
    }

    public void setPass_sender(String pass_sender) {
        this.pass_sender = pass_sender;
    }

    public String getRevicer() {
        return revicer;
    }

    public void setRevicer(String revicer) {
        this.revicer = revicer;
    }

    public String[] getRevicer_CC() {
        return revicer_CC;
    }

    public void setRevicer_CC(String[] revicer_CC) {
        this.revicer_CC = revicer_CC;
    }

    public String getFilename_attachment() {
        return filename_attachment;
    }

    public void setFilename_attachment(String filename_attachment) {
        this.filename_attachment = filename_attachment;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getMassage() {
        return massage;
    }

    public void setMassage(String massage) {
        this.massage = massage;
    }

    public SentMail(String sender, String pass_sender, String revicer, String filename_attachment, String subject, String massage, String[] revicer_CC) {
        this.sender = sender;
        this.pass_sender = pass_sender;
        this.revicer = revicer;
        this.filename_attachment = filename_attachment;
        this.subject = subject;
        this.massage = massage;
        this.revicer_CC = revicer_CC;
      
    }

   
    public int SentGmail() throws AddressException, MessagingException {
        try {
            String host = "smtp.gmail.com";//host name
            String from = this.sender;//sender idG
            String to = this.revicer;//reciever id
            String pass = this.pass_sender;//sender's password
            String fileAttachment = this.filename_attachment;//file name for attachment
            File file= new File(fileAttachment);
            String filename=file.getName();
            //system properties
            Properties prop = System.getProperties();
            // Setup mail server properties
            prop.put("mail.smtp.gmail", host);
            prop.put("mail.smtp.starttls.enable", "true");
            prop.put("mail.smtp.host", host);
            prop.put("mail.smtp.user", from);
            prop.put("mail.smtp.password", pass);
            prop.put("mail.smtp.port", "587");
            prop.put("mail.smtp.auth", "true");
            //session
            Session session = Session.getInstance(prop, null);
            // Define message
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            if (revicer_CC.length > 0) {
                for (String string : revicer_CC) {
                    message.addRecipient(Message.RecipientType.CC, new InternetAddress(string));
                }
            }
            message.setSubject(this.subject);
            // create the message part
            MimeBodyPart messageBodyPart = new MimeBodyPart();
            //message body
            messageBodyPart.setText(this.massage);
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);
            //attachment
            messageBodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource(fileAttachment);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(filename);
            multipart.addBodyPart(messageBodyPart);
            message.setContent(multipart);
            //send message to reciever
            Transport transport = session.getTransport("smtp");
            transport.connect(host, from, pass);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showConfirmDialog(null, "không gửi được email", "Thông báo", JOptionPane.CLOSED_OPTION);

        }
        return 0;
    }
}
